// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/functions/lib/map_concat.h>
#include <pollux/functions/lib/registration_helpers.h>
#include <pollux/functions/sparksql/size.h>

namespace kumo::pollux::functions {
    extern void registerElementAtFunction(
        const std::string &name,
        bool enableCaching);

    void registerSparkMapFunctions(const std::string &prefix) {
        POLLUX_REGISTER_VECTOR_FUNCTION(
            udf_map_allow_duplicates, prefix + "map_from_arrays");
        // Spark and Presto map_filter function has the same definition:
        //   function expression corresponds to body, arguments to signature
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_filter, prefix + "map_filter");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_entries, prefix + "map_entries");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_keys, prefix + "map_keys");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_values, prefix + "map_values");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_zip_with, prefix + "map_zip_with");

        registerMapConcatAllowSingleArg(prefix + "map_concat");
    }

    namespace sparksql {
        void registerMapFunctions(const std::string &prefix) {
            registerSparkMapFunctions(prefix);
            POLLUX_REGISTER_VECTOR_FUNCTION(udf_map, prefix + "map");
            // This is the semantics of spark.sql.ansi.enabled = false.
            registerElementAtFunction(prefix + "element_at", true);
            registerSize(prefix + "size");
        }
    } // namespace sparksql
} // namespace kumo::pollux::functions
